<script type="text/javascript">
$(function(){
	var pg = App.batch.infoEdit.ui, pp = pg.params;
	
	var frm = {
		catalog: pg.seek('catalog'),
		code: pg.seek('code'),
		name: pg.seek('name'),
		failable: pg.seek('failable'),
		freeze: pg.seek('freeze'),
		timeout: pg.seek('timeout'),
		params: pg.seek('params'),
		impl: pg.seek('impl'),
		cron: pg.seek('cron'),
		startDate: pg.seek('startDate'),
		stopDate: pg.seek('stopDate'),
		startHour: pg.seek('startHour'),
		stopHour: pg.seek('stopHour'),
		startMin: pg.seek('startMin'),
		stopMin: pg.seek('stopMin'),
		startSec: pg.seek('startSec'),
		stopSec: pg.seek('stopSec'),
// 		status: pg.seek('status'),
		job: pg.seek('job'),
		period: pg.seek('period'),
		memo: pg.seek('memo')
	};
	
	var dateCfg = {
		prevText: "<上一月",
		nextText: "下一月>",
		dateFormat: "yy-mm-dd",
		changeMonth: true,
		changeYear: true,
		inline: true
	};
	frm.startDate.datepicker(dateCfg);
	frm.stopDate.datepicker(dateCfg);
	
	frm.failable.mac('combo', {
		keyField: 'k',
		displayField: 'v',
		multiSelect: false,
		width: 340,
		boxWidth: 341,
		cols : [{
			field: 'k', width: 20
		}, {
			field: 'v', width: 320
		}],
		data: [{
			k: 0, v: '遇异常后无动作，批量直接失败'
		}, {
			k: 1, v: '忽略异常，不抛出（不中断父流程）'
		}, {
			k: 2, v: '遇异常后 续跑一次 当前批量，已成功的任务不再执行，失败的则会再跑一次'
		}, {
			k: 3, v: '遇异常后 重跑一次（从头开始，跑成功的子任务也再跑一遍） 当前批量'
		}, {
			k: 4, v: '前次执行必须完成且成功'
		}]
	});
	
	function dtfmt(d, h, m, s) {
		h = ((h*1)>9? '':'0') + h;
		m = ((m*1)>9? '':'0') + m;
		s = ((s*1)>9? '':'0') + s;
		return d + ' ' + h + ':' + m + ':' + s;
	}
	function getValues(){
		return {
			id: pg.params.id,
			catalog: frm.catalog.val(),
			code: frm.code.val(),
			name: frm.name.val(),
			failable: frm.failable.val(),
			freeze: frm.freeze.val(),
			timeout: frm.timeout.val(),
			params: frm.params.val(),
			impl: frm.impl.val(),
			cron: frm.cron.val(),
			startTime: dtfmt(frm.startDate.val(), frm.startHour.val(),
					frm.startMin.val(), frm.startSec.val()),
			stopTime: dtfmt(frm.stopDate.val(), frm.stopHour.val(),
					frm.stopMin.val(), frm.stopSec.val()),
// 			status: frm.status.val(),
			job: frm.job.val(),
			period: frm.period.val(),
			memo: frm.memo.val()
		};
	}

	function setValues(o, pom){
		pg.seek('title').html(o.id? '编辑批量定义':'添加批量定义');
		frm.catalog.val(o.catalog);
		frm.code.val(o.code);
		frm.name.val(o.name);
		frm.failable.val(o.failable);
		frm.freeze.val(o.freeze);
		frm.timeout.val(o.timeout);
		frm.params.val(o.params);
		frm.impl.val(o.impl);
		frm.cron.val(o.cron);
		var x = o.startTime, y = o.stopTime;
		if (x) {
			//2017-11-08 09:03:00
			frm.startDate.val(x.substr(0, 10));
			frm.startHour.val(x.substr(11, 2) * 1);
			frm.startMin.val(x.substr(14, 2) * 1);
			frm.startSec.val(x.substr(17, 2) * 1);
		}
		if (y) {
			frm.stopDate.val(x.substr(0, 10));
			frm.stopHour.val(x.substr(11, 2) * 1);
			frm.stopMin.val(x.substr(14, 2) * 1);
			frm.stopSec.val(x.substr(17, 2) * 1);
		}
// 		frm.status.val(o.status);
		frm.job.val(o.job);
		frm.period.val(o.period);
		frm.memo.val(o.memo);
	}
	function validate(p){
		var x2 = App.validate2, v = App.base.validation, m = pg.find('.msg').html('');
		var vd = true
			& !x2(v.app.code, frm.code)
			& !x2(v.app.catalog, frm.catalog)
			& !x2(v.app.name, frm.name);
		return vd;
	}
	pg.find('input').blur(function(){
		var pms = getValues();
		if (!validate(pms)) {
			return;
		}
	});
	// 禁用状态（未激活）的批量才允许修改
	if (pp.status == 0) {
		pg.seek('ok').click(function(){
			var pms = getValues();
			if(!validate(pms))
				return;
			$.post(App.ctx + '/info/save.do', pms, function(rs){
				var ro = mac.eval(rs);
				if (ro.success) {
					pg.callback(ro.data);//更新树	
					if(pg.close){
						pg.close();
					} else {
						//pg.callback(null, pg.params.id || pg.params.parent);
					}
				} else {
					pg.seek('msg').html(mac.msg(ro.data, ro.params));
				}
			});
			return false;
		}).removeAttr('disabled');
	}
	pg.seek('close').click(function(){
		pg.callback();
	});
	
//  	if (pg.params.id) {
// 		var w = mac.wait('loading'); 
// // 		if (pg.params.id) {
// // 			frm.code.attr('readonly', true);
// // 			frm.catalog.attr('readonly',true);
// // 		}
// 		$.get(App.ctx + '/info/get.do', {
// 			id : pg.params.id
//  		}, function(rs) {
//  			w.close();
//  			var ro = mac.eval(rs);
//  			if (ro.success) {
// 				setValues(ro.data);
//  			} else {
//  				pg.seek('msg').html(mac.msg(ro.data, ro.params));
//  			}
// 		}); 
// 	} else {
		setValues(pg.params);
// 	}
 	
	function adjust(){
		var h = pg.height();
		if(!pg.params.icon){
			pg.children('.form').height(h);
		}
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<div class="title" name="title"></div>
<div class="form">
	<div class="item">
		<span class="label">所属系统：</span>
		<input type="text" name="catalog" placeholder="所属系统" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">编号：</span>
		<input type="text" name="code" placeholder="系统内编号" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">名称：</span>
		<input type="text" name="name" placeholder="批量中文 名" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">失败策略：</span>
		<div class="combo fl" name="failable" placeholder="批量中文 名"></div>
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">冻结时间：</span>
		<input type="text" name="freeze" placeholder="冻结时间（单位：分钟）" value="0" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">超时时间：</span>
		<input type="text" name="timeout" placeholder="超时时间（单位：分钟）" value="0" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">初始参数：</span>
		<textarea name="params"></textarea>
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">实现类：</span>
		<input type="text" name="impl" placeholder="实现类" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">cron表达式：</span>
		<input type="text" name="cron" placeholder="cron表达式" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">生效时间：</span>
		<input type="text" name="startDate" style="width: 180px" placeholder="定时计划生效时间" />
		<input type="number" name="startHour" style="margin-left: 12px; width: 40px" placeholder="时" />
		<span class="fl">:</span>
		<input type="number" name="startMin" style="width: 40px" placeholder="分" />
		<span class="fl">:</span>
		<input type="number" name="startSec" style="width: 40px" placeholder="秒" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">失效时间：</span>
		<input type="text" name="stopDate" style="width: 180px" placeholder="定时计划失效时间" />
		<input type="number" name="stopHour" style="margin-left: 12px; width: 40px" placeholder="时" />
		<span class="fl">:</span>
		<input type="number" name="stopMin" style="width: 40px" placeholder="分" />
		<span class="fl">:</span>
		<input type="number" name="stopSec" style="width: 40px" placeholder="秒" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">QuartzJob：</span>
		<input type="text" name="job" placeholder="QuartzJob类" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">间隔时间：</span>
		<input type="text" name="timeout" placeholder="间隔时间（单位：分钟）" value="0" />
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="item">
		<span class="label">备注：</span>
		<textarea name="memo"></textarea>
		<span class="msg">*</span>
		<div class="clear"></div>
	</div>
	<div class="bottom">
		<button name="ok" disabled="disabled">确定</button>
		&nbsp;&nbsp;
		<button name="close">取消</button>
	</div>
</div>